package projectsupporter.common.operation;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

import projectsupporter.common.control.Path;
import projectsupporter.common.logic.PlugLogic;

import dbTools.XML.ValidatorXMLService;
import dbTools.XML.XMLService;
import dbTools.db.SQLService;
import dbTools.db.writer.writers.MySQLWriter;
import dbTools.db.writer.writers.OracleWriter;
import dbTools.pojo.JavaBeanCreator;
import dbTools.tableInfo.info.TableInfo;

public class CreateBaseCodeOperation implements IRunnableWithProgress{

	private Map<String,TableInfo> tableInfos;
	
	private boolean floderByIDcbx;
	
	private Path abHibPath;
	private Path abPojoPath;
	private Path abValiPath;
	private Path abSqlPath;
	
	
	
	public CreateBaseCodeOperation(Map<String, TableInfo> tableInfos,
			boolean floderByIDcbx, Path abHibPath, Path abPojoPath,
			Path abValiPath,Path abSqlPath) {
		super();
		this.tableInfos = tableInfos;
		this.floderByIDcbx = floderByIDcbx;
		this.abHibPath = abHibPath;
		this.abPojoPath = abPojoPath;
		this.abValiPath = abValiPath;
		this.abSqlPath = abSqlPath;
	}



	@Override
	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		
		monitor.beginTask("Create base code ", IProgressMonitor.UNKNOWN);
		//hibernate mapping files
		if(abHibPath!=null)
		{
			if(tableInfos!=null && tableInfos.size()>0)
			{
				monitor.subTask("hibernate mapping files");
				
				List<String> mappingFilesPath= XMLService.createHibernateMapingFiles(tableInfos,abHibPath,floderByIDcbx);
				
				monitor.worked(30);
				if(mappingFilesPath!=null && mappingFilesPath.size()>0)
				{
					monitor.subTask("add mipping config");
					PlugLogic.addMappingResources(mappingFilesPath);
					monitor.worked(2);
				}
			}
		}
		//pojos
		if(abPojoPath!=null)
		{
			if(tableInfos!=null && tableInfos.size()>0)
			{
				monitor.subTask("create pojos");
				
				Map<String,String> pojoClassMap=JavaBeanCreator.createPOJOs(tableInfos,abPojoPath,floderByIDcbx);
				monitor.worked(10);
				
				monitor.subTask("add pojo config");
				PlugLogic.addPojoClassConfig(pojoClassMap);
				monitor.worked(2);
			}
		}
		
		//validators
		if(abValiPath!=null)
		{
			if(tableInfos!=null && tableInfos.size()>0)
			{
				monitor.subTask("create validators");
				
				ValidatorXMLService.createValidatorXMLFiles(tableInfos,abValiPath,floderByIDcbx);
				monitor.worked(10);
			}
		}
		
		//SQL Scripts
		if(tableInfos!=null && tableInfos.size()>0)
		{
			monitor.subTask("create SQL Scripts");
			
			//MySQL
			//MySQLWriter writer = new MySQLWriter();
			
			//Oracle
			OracleWriter writer = new OracleWriter();
			
			
			SQLService.createSQLFile(tableInfos,abSqlPath,floderByIDcbx,writer);
			monitor.worked(5);
		}
		
		monitor.done();
	}

}
